package com.kalvan.web.dict;

import com.kalvan.remoteserver.bean.res.DictDto;
import com.kalvan.remoteserver.iface.IBaseRemote;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author chenliang
 */
@Component
public class DictCache {

    /**
     * dictType , dictKey-dictValue
     */
    Map<String, Map<String, DictDto>> dictAll = new LinkedHashMap<>();

    /**
     * dubbo 基础服务
     */
    @Reference
    private IBaseRemote baseRemote;

    /**
     * 初始化并完成全局缓存赋值
     *
     * @return
     * @see
     */
    @Bean
    private DictCache readConf() {
        CacheUtil.cache = this;
        return this;
    }

    /**
     * 获取某类字典
     *
     * @param dictType
     * @return dictKey:dictValue
     * @see
     */
    public Map<String, DictDto> getDictMap(String dictType) {
        Map<String, DictDto> map = dictAll.get(dictType);
        if (map == null) {
            map = baseRemote.getDictMap(dictType);
            if (map != null) {
                map = sortMapByKey(map);
                dictAll.put(dictType, map);
            }
        }
        return map;
    }

    /**
     * 获取某类字典缓存
     *
     * @param dictType
     * @return Collection
     * @see
     */
    public List<DictDto> getDict(String dictType) {
        Map<String, DictDto> map = this.getDictMap(dictType);
        if (map != null) {
            return new ArrayList<DictDto>(this.getDictMap(dictType).values());
        }
        return new ArrayList<DictDto>();
    }

    /**
     * 获取字典值
     *
     * @param dictType
     * @param dictKey
     * @return
     * @see
     */
    public String getDictValue(String dictType, String dictKey) {
        Map<String, DictDto> map = this.getDictMap(dictType);
        if (map != null && map.get(dictKey) != null) {
            return map.get(dictKey).getDictValue();
        }
        return "";
    }

    /**
     * 获取业务配置参数
     *
     * @param dictKey
     * @return dictValue
     * @see
     */
    public String getBusinessCahce(String dictKey) {
        return this.getDictValue(SystemDictType.CACHE_BUSINESS, dictKey);
    }

    /**
     * 获取业务配置参数
     *
     * @param dictKey
     * @return dictValue
     * @see
     */
    public String getSystemCahce(String dictKey) {
        return this.getDictValue(SystemDictType.CACHE_SYSTEM, dictKey);
    }

    public void updateDictCache(String dictType) {
        if (StringUtils.isNotBlank(dictType)) {
            dictAll.remove(dictType);
        } else {
            dictAll.clear();
        }
    }

    /**
     * 使用 Map按key进行排序
     *
     * @param map
     * @return
     */
    public Map<String, DictDto> sortMapByKey(Map<String, DictDto> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        Map<String, DictDto> sortMap = new TreeMap<String, DictDto>(new MapKeyComparator());

        sortMap.putAll(map);

        return sortMap;
    }

    class MapKeyComparator implements Comparator<String> {

        @Override
        public int compare(String str1, String str2) {

            return str1.compareTo(str2);
        }
    }
}
